package com.neusoft.resturant.controller;


import com.alibaba.fastjson2.JSON;
import com.neusoft.resturant.entity.ResAddress;
import com.neusoft.resturant.entity.ResBanner;
import com.neusoft.resturant.entity.SysUser;
import com.neusoft.resturant.service.IResAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 地址Controller
 * 
 * @author ruoyi
 * @date 2023-12-10
 */
@RestController
@RequestMapping("/restaurant/address")
public class ResAddressController extends BaseController
{
    @Autowired
    private IResAddressService resAddressService;


    /**
     * 新增地址
     */
    @PostMapping("/add")
    public int add(@RequestBody ResAddress resAddress)
    {
        String token = request.getHeader("token");
        // 从Redis中获取use_id
        String  userJson = (String) redisTemplate.opsForValue().get(token);
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        String userName=sysUser.getUserName();

        // 如果use_id为空，则返回错误或采取其他适当的操作
        if (userName == null ) {
            return -1; // 或者其他表示错误的值
        }

        // 将use_id设置到resComment对象中
        resAddress.setCreateBy(userName);
        System.out.println("新增成功");
        return (resAddressService.insertResAddress(resAddress));
    }

    /**
     * 修改地址
     */

    @PutMapping("/edit")
    public int edit(@RequestBody ResAddress resAddress)
    {
        System.out.println("修改成功");
        return (resAddressService.updateResAddress(resAddress));
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/remove/{id}")
    public int remove(@PathVariable Long[] id)
    {
        return (resAddressService.deleteResAddressByIds(id));
    }
}
